package demo.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import demo.entity.Manager;
import demo.util.DBUtil;
import demo.util.MD5Util;

public class ManagerDao {
	public Manager findById(String id) {
		Connection con = DBUtil.getConnection();
		String selectManagerSqlStr = "SELECT * FROM manager where id='" + id + "'";
		if (con != null) {
			try {
				ResultSet rs = con.createStatement().executeQuery(selectManagerSqlStr);
				List<Manager> managerList = rsToManager(rs);
				if (managerList != null && managerList.size() != 0)
					return managerList.get(0);
			} catch (SQLException e) {
				processSqlError(e);
			} finally {
				DBUtil.closeConnection(con);
			}
		}
		return null;
	}
	
	private List<Manager> rsToManager(ResultSet rs) {
		List<Manager> managerList = new ArrayList<>();
		try {
			while (rs.next()) {
				String id = rs.getString(1);
				String name=rs.getString(2);
				String password = rs.getString(3);
				String tel=rs.getString(4);
				Manager manager = new Manager(id, password,name,tel);
				managerList.add(manager);
			}
			return managerList;
		} catch (SQLException e) {
			processSqlError(e);
		}
		return null;
	}
	
	private void processSqlError(Exception e) {
		e.printStackTrace();
	}
	

	
	

	
}
